/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tramite.converters;

import cip.util.ControladorAbstracto;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import org.cip.esymons.tramite.be.Tramite;
import org.cip.esymons.tramite.beans.TramiteController;
/**
 *
 * @author Cristian
 */
public class TramiteConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null) {
            return new Tramite();
        }
        if (value.equals("Todos")) {
            return new Tramite();
        }
        try {
            Long id = new Long(value);
            if (id == 0) {
                return null;
            }
            TramiteController controller = (TramiteController) ControladorAbstracto.getBean("tramiteController");
            return controller.buscarXId(id);
        } catch (Exception ex) {
            return new Tramite();
        }
    }

    public String getAsString(FacesContext context, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Tramite) {
            Tramite t = (Tramite) object;
            return "" + t.getTrmCodigo();
        } else {
            return null;
        }
    }
}